﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web;

namespace Yuanlin.Business
{
    public class UserContact : YuanlinUserControl,
        IReplyConverter<CompanyCardReply, GetListOperation>
    {
        protected Repeater list;
        private EntityList companyCards;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetCompanyCardListRequest(this,UserIdFromGreenNum, CompanyCardFlag.FavoritesCard, 0, 0, 100));
        }

        protected override void OnLoad(EventArgs e)
        {
            AjaxManager.Register(this, "UserContact");
        }

        private void BindData()
        {
            if (list == null)
            {
                return;
            }

            list.DataSource = companyCards;
            list.DataBind();
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int UpdateCompanyCardFlag(string companyCard)
        {
            if (user.IsAnonymous)
                return -1;//到登陆页面

            string companyCardFlag = companyCard.Split(':')[0];
            string companyName= companyCard.Split(':')[1];
            int companyCardEntityId = Int32.Parse(companyCard.Split(':')[2]);
            int returnInt = 0;
            CompanyCardListRequest request = null;
            switch (companyCardFlag)
            {
                case "1":
                    {
                        //收藏名片
                        request = new CompanyCardListRequest();
                        request.UserId = user.EntityId.Value;
                        request.CompanyName = companyName;
                        request.Flag = CompanyCardFlag.FavoritesCard;
                        request.PageIndex = 0;
                        request.PageSize = 1;
                        EntityList companyCards = BusinessManager.GetCompanyCardList(RequestHelper.BuildGetCompanyCardListRequest(request));
                        if (companyCards.Count > 0)
                            return 11;//已经收藏过该名片

                        //如果已经递交过，就直接更新
                        request = new CompanyCardListRequest();
                        request.UserId = user.EntityId.Value;
                        request.CompanyName = companyName;
                        request.Flag = CompanyCardFlag.SubmitCard;
                        request.PageIndex = 0;
                        request.PageSize = 1;
                        EntityList companyCard1s = BusinessManager.GetCompanyCardList(RequestHelper.BuildGetCompanyCardListRequest(request));
                        if (companyCard1s.Count > 0)
                        {
                            CompanyCard companyCardObj = companyCard1s[0] as CompanyCard;
                            companyCardObj.Flag.Value = (int)CompanyCardFlag.FavoritesCard;
                            BusinessManager.UpdateCompanyCard(companyCardObj);
                        }
                        else
                        {
                            AddCompanyCard(CompanyCardFlag.FavoritesCard, companyCardEntityId);
                        }
                        returnInt = 1;
                    }
                    break;
                case "2":
                    {
                        //递交名片
                        request = new CompanyCardListRequest();
                        request.UserId = user.EntityId.Value;
                        request.CompanyName = companyName;
                        request.Flag = CompanyCardFlag.SubmitCard;
                        request.PageIndex = 0;
                        request.PageSize = 1;
                        EntityList companyCards = BusinessManager.GetCompanyCardList(RequestHelper.BuildGetCompanyCardListRequest(request));
                        if (companyCards.Count > 0)
                            return 22;//已经递交过该名片

                        request = new CompanyCardListRequest();
                        request.UserId = user.EntityId.Value;
                        request.CompanyName = companyName;
                        request.Flag = CompanyCardFlag.FavoritesCard;
                        request.PageIndex = 0;
                        request.PageSize = 1;
                        companyCards = BusinessManager.GetCompanyCardList(RequestHelper.BuildGetCompanyCardListRequest(request));
                        if (companyCards.Count > 0)
                            return 23;//已经递交过该名片

                        AddCompanyCard(CompanyCardFlag.SubmitCard, companyCardEntityId);
                        returnInt = 2;
                    }
                    break;
            }
            return returnInt;
        }

        private void AddCompanyCard(CompanyCardFlag flag, int companyCardEntityId)
        {
            CompanyCard companyCard = BusinessManager.GetCompanyCard(companyCardEntityId);
            companyCard.UserId.Value = user.EntityId.Value;
            companyCard.CompanyUserId.Value = UserIdFromGreenNum;
            companyCard.IsRegisterCompany.Value = 0;
            companyCard.Flag.Value = (int)flag;
            BusinessManager.CreateCompanyCard(companyCard);
        }

        #endregion

        #region IReplyConverter<CompanyCardReply,GetListOperation> Members

        public void ConvertReply(CompanyCardReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                companyCards = reply.EntityList;
                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}
